<?php
class UsersController extends AppController {
	public $helpers = array (
			'Html',
			'Form',
			'Session'
	);
	
	public function login(){
		if($this->Session->check('userLogin')){
			$this->Session->setFlash ('Please input search condition!', 'default', array (
					'class' => 'alert alert-danger'));
			return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
		} 
		
		if($this->request->is('post')){
			if(!$this->request->data['username']){
				$this->Session->setFlash ('Please enter username!', 'default', array (
						'class' => 'alert alert-danger'));
				return;
			}
			
			if(!$this->request->data['password']){
				$this->Session->setFlash ('Please enter password!', 'default', array (
						'class' => 'alert alert-danger'));
				return;
			}
			
			$username = $this->request->data['username'];
			$password = $this->request->data['password'];
			
			$user = $this->User->find('first', array (
				'conditions' => array ('User.username' => $username)));
			
			if(!$user){
				$this->Session->setFlash ('Username or password is incorrect!', 'default', array (
						'class' => 'alert alert-danger'));
				return;
			}
			
			if($user['User']['password'] != $password){
				$this->Session->setFlash ('Username or password is incorrect!', 'default', array (
						'class' => 'alert alert-danger'));
				return;
			}else{
				$this->Session->write('userLogin', $user);
				$this->Session->setFlash ('Login success! Welcome '.$user['User']['fullname'].'!', 'default', array (
						'class' => 'alert alert-success'));
				return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
			}			
		}
	}
	
	public function logout(){
		if(!$this->Session->check('userLogin')){
			$this->Session->setFlash ('You are not logged in. Please login again!', 'default', array (
					'class' => 'alert alert-danger'));
			return $this->render("/users/login");
			
		}else{
			$this->Session->delete('userLogin');
			$this->Session->setFlash ('Logout success!', 'default', array (
					'class' => 'alert alert-success'));
			return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
		}
	}
	
	/**
	 * Method register
	 */
	public function register() {
		//check login
		if($this->Session->check('userLogin')){
			$this->Session->setFlash ('You already have an account!!', 'default', array (
					'class' => 'alert alert-danger'));
			return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
		} 
		
		//validate input
		if($this->request->is('post')) {
			$username = $this->request->data['username'];
			$admin = $this->User->find('first',array('conditions' => array('User.username' => $username)));
			if($admin) {
				$this->Session->setFlash('Username had been used!!!','default',array('class'=>'alert alert-warning'));
				return $this->render('/users/register');
			}
			$pass = $this->request->data['password'];
			$confirm = $this->request->data['confirmPassword'];
			if($pass != $confirm) {
				$this->Session->setFlash('Confirm password is not correct!!!','default',array('class'=>'alert alert-warning'));
				return $this->render('/users/register');
			}
			//save to db
			$this->User->set(array(
					'username' => $this->request->data['username'],
					'password' => $this->request->data['password'],
					'fullname' => $this->request->data['fullname']
			));
			if($this->User->save()) {
				$this->Session->setFlash('Register successfully.','default',array('class'=>'alert alert-success'));
				return $this->redirect(array('action' => 'doLoginAfterRegis', $this->request->data['username']));
			}
			$this->Session->setFlash('Register failed!!!','default',array('class'=>'alert alert-warning'));
			return $this->render('/users/register');
		}
	}
	
	/**
	 * Method login after register
	 */
	private function doLoginAfterRegis($username = null) {
		//check params
		if(!$username) {
			$this->Session->setFlash('Invalid transaction!!!','default',array('class'=>'alert alert-warning'));
			return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
		}
		//initiate session for logged-in
		$this->Session->write('userLogin',$this->User->findByUsername($username));
		return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
	}
}